package cn.lele.reggie.service.impl;

import cn.lele.reggie.dto.SetmealDto;
import cn.lele.reggie.entity.Category;
import cn.lele.reggie.entity.Dish;
import cn.lele.reggie.entity.Setmeal;
import cn.lele.reggie.entity.SetmealDish;
import cn.lele.reggie.mapper.SetmealMapper;
import cn.lele.reggie.service.CategoryService;
import cn.lele.reggie.service.DishService;
import cn.lele.reggie.service.SetmealDishService;
import cn.lele.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize,String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(setmealPage,wrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal setmeal : setmealPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            setmealDtos.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        log.info("setmealDto:"+setmealDto);
        this.save(setmealDto);
        Setmeal setmeal = this.getById(setmealDto);
        Long setmealId = setmeal.getId();
        //将一个或多个菜品保存到套餐菜品表
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealId);
            setmealDishService.save(setmealDish);
        }
        redisTemplate.delete("Setmeal_"+setmealDto.getCategoryId());
    }

    @Override
    @Transactional
    public void deleteWithDish(Long[] ids) {
        //id是套餐的id
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            Setmeal setmeal = getById(id);
            this.removeById(id);
            //删除套餐中的菜品
            wrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(wrapper);
            redisTemplate.delete("Setmeal_"+setmeal.getCategoryId());
        }
    }
    @Override
    public void updateStatus(Integer statusId, Long[] ids) {
        String key=null;
        //改为禁止销售
        if(statusId==0){
            log.info("要禁止了。。。。。。。。。。。。。");
            for (Long id : ids) {
                Setmeal setmeal = this.getById(id);
                setmeal.setStatus(0);
                this.updateById(setmeal);
                key="Setmeal_"+setmeal.getCategoryId();
                redisTemplate.delete(key);
            }
        }else{
            log.info("要开始售卖了。。。。。。。。。。。。。");
            for (Long id : ids) {
                Setmeal setmeal = this.getById(id);
                setmeal.setStatus(1);
                this.updateById(setmeal);
                key="Setmeal_"+setmeal.getCategoryId();
                redisTemplate.delete(key);
            }
        }
    }

    @Override
    public SetmealDto selectDtoById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishListlist = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishListlist);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateDto(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        //根据套餐的id删除setmealdish中对应的菜品
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wrapper);
        //将改后的数据添加到setmealDish
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        }
        redisTemplate.delete("Setmeal_"+setmealDto.getCategoryId());
    }

    @Override
    public List<SetmealDto> listSetmeal(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> list = this.list(wrapper);
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal setmeal : list) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            setmealDtos.add(setmealDto);
        }
        return setmealDtos;
    }

    @Override
    public List<SetmealDish> listDish(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        ArrayList<SetmealDish> setmealDishes = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishLambdaQueryWrapper.eq(Dish::getId,setmealDish.getDishId());
            Dish dish = dishService.getOne(dishLambdaQueryWrapper);
            setmealDish.setImage(dish.getImage());
            setmealDishes.add(setmealDish);
        }
        return setmealDishes;
    }
}
